maze_gen.h
#ifndef MAZE_GEN_H
#define MAZE_GEN_H
typedef struct {
int width, height;
char **grid; // '#' wall, ' ' corridor, 'S' start, 'E' end, '.' path
int startX, startY;
int endX, endY;
} Maze;
void initMaze(Maze *maze, int w, int h);
void generateMaze(Maze *maze);
void freeMaze(Maze *maze);
#endif
maze_gen.c
#include "maze_gen.h"
#include <stdlib.h>
#include <time.h>
static void shuffle_dirs(int dirs[4][2]) {
for (int i = 0; i < 4; i++) {
int r = rand() % 4;
int tx = dirs[i][0], ty = dirs[i][1];
dirs[i][0] = dirs[r][0]; dirs[i][1] = dirs[r][1];
dirs[r][0] = tx; dirs[r][1] = ty;
}
}
static void carve(int x, int y, Maze *maze) {
int dirs[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
shuffle_dirs(dirs);
for (int i = 0; i < 4; i++) {
int nx = x + dirs[i][0]*2;
int ny = y + dirs[i][1]*2;
if (nx > 0 && ny > 0 && nx < maze->width-1 && ny < maze->height-1
&& maze->grid[ny][nx] == '#') {
maze->grid[y + dirs[i][1]][x + dirs[i][0]] = ' ';
maze->grid[ny][nx] = ' ';
carve(nx, ny, maze);
}
}
}
void initMaze(Maze *maze, int w, int h) {
if (w % 2 == 0) w++; // ensure odd
if (h % 2 == 0) h++;
maze->width = w;
maze->height = h;
maze->grid = malloc(h * sizeof(char *));
for (int i = 0; i < h; i++) {
maze->grid[i] = malloc(w * sizeof(char));
for (int j = 0; j < w; j++) maze->grid[i][j] = '#';
}
maze->startX = 1;
maze->startY = 1;
maze->endX = w - 2;
maze->endY = h - 2;
}
void generateMaze(Maze *maze) {
srand((unsigned)time(NULL));
maze->grid[maze->startY][maze->startX] = ' ';
carve(maze->startX, maze->startY, maze);
maze->grid[maze->startY][maze->startX] = 'S';
maze->grid[maze->endY][maze->endX] = 'E';
}
void freeMaze(Maze *maze) {
for (int i = 0; i < maze->height; i++) free(maze->grid[i]);
free(maze->grid);
}
display.h
#ifndef DISPLAY_H
#define DISPLAY_H
#include "maze_gen.h"
void displayMaze(const Maze *maze);
void displayMazeWithPlayer(const Maze *maze, int px, int py);
void displayMazeWithAIPath(const Maze *maze); // display final grid (with '.' marks)
void animateAIPath(Maze *maze, int path[][2], int len, int delay_ms);
#endif
display.c
#include "display.h"
#include <stdio.h>
#include <unistd.h> // for usleep
void displayMaze(const Maze *maze) {
for (int y = 0; y < maze->height; y++) {
for (int x = 0; x < maze->width; x++) {
putchar(maze->grid[y][x]);
}
putchar('\n');
}
}
void displayMazeWithPlayer(const Maze *maze, int px, int py) {
for (int y = 0; y < maze->height; y++) {
for (int x = 0; x < maze->width; x++) {
if (x == px && y == py) putchar('P');
else putchar(maze->grid[y][x]);
}
putchar('\n');
}
}
void displayMazeWithAIPath(const Maze *maze) {
// shows grid including '.' as path
displayMaze(maze);
}
void animateAIPath(Maze *maze, int path[][2], int len, int delay_ms) {
// path is ordered from goal back to start (or start to goal) depending caller.
// We'll display step-by-step by marking '.' progressively.
for (int i = len - 1; i >= 0; i--) {
int x = path[i][0], y = path[i][1];
if (maze->grid[y][x] == ' ') maze->grid[y][x] = '.';
// clear screen (ANSI)
printf("\x1b[2J\x1b[H");
displayMaze(maze);
usleep(delay_ms * 1000); // delay_ms milliseconds
}
}